package com.scalar.chiptrack.finance.openpo.actioncommands;

import java.util.HashMap;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackExtendedAction;
import com.scalar.chiptrack.finance.openpo.forms.PurchaseOrderSearchForm;
import com.scalar.chiptrack.finance.openpo.dao.PurchaseOrderDAO;

import com.scalar.chiptrack.users.ErrorReport;
import com.scalar.chiptrack.utils.Logger;

/**
 * Author: Phani Raja Kumar
 * Date  : Feb 17, 2004
 * Time  : 5:34:03 PM
 */
public class OpenPurchaseOrderSearchCommand extends ChipTrackExtendedAction {

    public ActionForward executeRequest( ActionMapping mapping,
                                         ActionForm form,
                                         HttpServletRequest request,
                                         HttpServletResponse response ) throws Exception
    {

        HashMap errors              = ( HashMap ) request.getAttribute( ERROR_MESSAGES );
        HashMap info                = ( HashMap ) request.getAttribute( INFO_MESSAGES );
		PurchaseOrderSearchForm purchaseOrderSearchForm = ( PurchaseOrderSearchForm )form;
		String actionForward 		= "Search";
		String search 				= request.getParameter( ACTION_SEARCH );
		HttpSession mSession 		= request.getSession();

        if( errors == null )
        {
			errors	= new HashMap(); 
        }

		if( info == null )
		{
			info	= new HashMap(); 
		}


		try
        {
            request.setAttribute( "vendorsList", PurchaseOrderDAO.loadAllVendors() );
            request.setAttribute ( "years", PurchaseOrderDAO.getYearList () );
			
			if( search != null )
			{
            	ArrayList purchaseOrderSearchList = PurchaseOrderDAO.searchOpenPO( purchaseOrderSearchForm );

	            if( purchaseOrderSearchList != null && purchaseOrderSearchList.size() != 0 )
	            {
	                purchaseOrderSearchForm.setPurchaseOrderSearchList( purchaseOrderSearchList );
	            }
	            else
	            {
	                info.put( INFO_PURCHASE_ORDERS_WITH_SEARCH_CRITERIA_NOT_FOUND, null );
	            }
			}
			
            actionForward = "OpenPOSearch";
       	}
        catch ( Exception e )
        {
            Logger.error( ERROR_LOADING_PURCHASE_ORDER_INFORMATION, e );
            errors.put( ERROR_INTERNAL_SERVER_ERROR, ERROR_LOADING_PURCHASE_ORDER_INFORMATION );
			mSession.setAttribute( ERROR_REPORT_OBJECT, new ErrorReport( "Error while executing the command", e ) );
            actionForward = "Search";
        }

		request.setAttribute( ERROR_MESSAGES, errors );
		request.setAttribute( INFO_MESSAGES, info );
		return mapping.findForward( actionForward );
    }

}
